package com.ckeri.boot.io.common.pay.comtroller;

import com.ckeri.boot.io.common.pay.config.WechatAccountConfig;
import com.lly835.bestpay.enums.BestPayPlatformEnum;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.lly835.bestpay.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Slf4j
public class PayControllerDemo {

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Autowired
    private BestPayServiceImpl bestPayService;

    @GetMapping("/")
    public ModelAndView index() {
        System.out.println("跳转页面");
        return new ModelAndView("index");
    }
    /**
     * 发起支付
     * pc网页支付 不需要openid
     * alipay_pc  支付宝pc
     * NATIVE   微信Native支付
     */
    @RequestMapping(value = "/pay")
    @ResponseBody
    public PayResponse pay(@RequestParam(value = "openid", required = false) String openid,
                           @RequestParam BestPayTypeEnum payType,
                           @RequestParam String orderId,
                           @RequestParam Double amount,
                           @RequestParam(required = false) String buyerLogonId,
                           @RequestParam(required = false) String buyerId) {
        //支付请求参数
        PayRequest request = new PayRequest();
        request.setPayTypeEnum(payType);
        request.setOrderId(orderId);
        request.setOrderAmount(amount);
        request.setOrderName("最好的支付sdk");
        request.setOpenid(openid);
        request.setAttach("这里是附加的信息");

        if (payType == BestPayTypeEnum.ALIPAY_H5) {
            request.setBuyerLogonId(buyerLogonId);
            request.setBuyerId(buyerId);
        }

        log.info("【发起支付】request={}", JsonUtil.toJson(request));

        PayResponse payResponse = bestPayService.pay(request);


        log.info("【发起支付】response={}", JsonUtil.toJson(payResponse));
        return payResponse;
    }




    /**
     * 退款
     *
     * @param orderId
     * @return
     * http://localhost:8087/refund?orderId=1593743517006286681&payType=ALIPAY_PC
     */
    @GetMapping("/refund")
    @ResponseBody
    public RefundResponse refund(@RequestParam String orderId, @RequestParam BestPayTypeEnum payType ) {
        RefundRequest request = new RefundRequest();

        request.setPayTypeEnum(payType);
        request.setOrderId(orderId);
        request.setOrderAmount(0.1);
        RefundResponse response = bestPayService.refund(request);
        return response;
    }



    /**
     * 异步回调
     */
    @PostMapping(value = "/notify")
    public ModelAndView notify(@RequestBody String notifyData) {
        log.info("【异步通知】支付平台的数据request={}", notifyData);
        PayResponse response = bestPayService.asyncNotify(notifyData);
        log.info("【异步通知】处理后的数据data={}", JsonUtil.toJson(response));

        //返回成功信息给支付平台，否则会不停的异步通知
        if (response.getPayPlatformEnum() == BestPayPlatformEnum.WX) {
            return new ModelAndView("pay/responeSuccessForWx");
        } else if (response.getPayPlatformEnum() == BestPayPlatformEnum.ALIPAY) {
            System.out.println("跳转 成功页面Ali");
            return new ModelAndView("pay/responeSuccessForAlipay");
        }
        throw new RuntimeException("错误的支付平台");
    }


}
